<script>
import { getConfit, getUserInfo, add_user_log } from 'common/api.js';
import { initWxConfig, setIndexPagetoShare } from 'common/index.js';
import store from 'store/index.js';
export default {
	onLaunch: function() {
		uni.removeStorageSync('topHeight');
		//#ifndef H5
		// getUserInfo().then(({ data }) => {
		// 	store.commit('setUserInfo', data.user_info);
		// });
		//#endif
		//#ifdef APP-PLUS
		//#endif
	},
	onShow: function() {
        //#ifdef H5
        this.handleGetConfig();
        //#endif
		// 进入小程序统计访问次数
		if (store.state.token) {
			this.$store.commit('get_unread');
			const token = store.state.token;
			/* add_user_log({ token }).then(res => {
				console.log('访问了一下');
			}); */
		}
		// 微信小程序自动检测是否有新版本更新
		//#ifdef MP
		const updateManager = uni.getUpdateManager();
		updateManager.onCheckForUpdate(function(res) {
			// 请求完新版本信息的回调
			if (res.hasUpdate) {
				updateManager.onUpdateReady(function(res2) {
					uni.showModal({
						title: '更新提示',
						content: '发现新版本，是否重启应用?',
						cancelColor: '#eeeeee',
						confirmColor: '#FF0000',
						success(res2) {
							if (res2.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					});
				});
			}
		});
		updateManager.onUpdateFailed(function(res) {
			// 新的版本下载失败
			uni.showModal({
				title: '提示',
				content: '检查到有新版本，但下载失败，请检查网络设置',
				success(res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						updateManager.applyUpdate();
					}
				}
			});
		});
		//#endif
	},
	onHide() {
		uni.removeStorageSync('topHeight');
	},
	methods: {
		// 设置微信配置
		handleGetConfig() {
            if (store.state.token) {
                let url = encodeURIComponent(window.location.href);
                getConfit({ url }).then(({ data }) => {
                    console.log('data', data);
                    initWxConfig(data);
                    setIndexPagetoShare();
                    if (store.state.hasLogin) {
                        getUserInfo().then(({ data }) => {
                            store.commit('setUserInfo', data.user_info);
                        });
                    }
                });
            }
		}
	}
};
</script>

<style lang="scss">
// #ifndef APP-MP (条件编译, 这里的注释符号是必须的)
@import 'styles/iconfont.scss';
@import 'styles/iconfont.css';
//@import 'styles/bootstrap.min.css';
/* 导入全局样式 自定义*/
@import '/static/css/common.css';

/*每个页面公共css */
page {
	background-color: #f5f5f5;
	height: 100%;
}
@font-face {
	font-family: PingFang SC;
}
image {
	display: block;
}
view,
scroll-view,
swiper,
swiper-item,
cover-view,
cover-image,
icon,
text,
rich-text,
progress,
button,
checkbox,
form,
input,
label,
radio,
slider,
switch,
textarea,
navigator,
audio,
camera,
image,
video {
	box-sizing: border-box;
}
.bold {
	font-weight: bold;
}
.flex-wrap {
	display: flex;
	flex-direction: flex-wrap;
	align-items: center;
}
.flex-column {
	display: flex;
	flex-direction: column;
	align-items: center;
}
button {
	&:after {
		border: 0;
	}
}
.assemble-btn {
	color: #fff;
	font-size: 28rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
.empty-btn {
	font-size: 28rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
// #endif
</style>
